//
//  HBQHomeManager.m
//  HelloBeeQuick
//
//  Created by LiYiLin on 16/8/20.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "HBQHomeManager.h"
#import "HBQNetWorkTools.h"
#import "HBQHeaderModel.h"

#define KHomeDataUrl @"http://iosapi.itcast.cn/loveBeen/focus.json.php"

@implementation HBQHomeManager

+ (instancetype)sharedManager {
    
    static HBQHomeManager *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HBQHomeManager alloc] init];
    });
    return instance;
}

- (void)loadHomeData:(void (^)(NSArray *))completeBlock {
    
    //设置请求网络数据上传的参数
    NSDictionary *parameters = @{@"call" : @1};
    
    [[HBQNetWorkTools SharedManager]post_RequestWithUrl:KHomeDataUrl parameters:parameters successBlock:^(id responseObjects) {
        
        //创建字典保存从网络上请求下来的数据
        NSDictionary *dataDic = responseObjects[@"data"];
        
        //保存轮播器的数据
        NSArray *focus = dataDic[@"focus"];
        //保存按钮的数据
        NSArray *icons = dataDic[@"icons"];
        //保存新特惠的数据
        NSArray *activities = dataDic[@"activities"];
        
        //字典转模型
        //保存轮播器的数据
        NSArray *focusModelArray = [HBQHeaderModel dataWithArray:focus];
        //保存按钮的数据
        NSArray *iconsModelArray = [HBQHomeIconsModel dataWithArray:icons];
        //保存新特惠的数据
        NSArray *activitiesModelArray = [HBQActivitesModel dataWithArray:activities];
        
        //用一个数据按顺序装载全部的数据
        NSMutableArray *totalDtoArray = [NSMutableArray arrayWithCapacity:3];
        [totalDtoArray addObject:focusModelArray];
        [totalDtoArray addObject:iconsModelArray];
        [totalDtoArray addObject:activitiesModelArray];
        
        //利用block向外界传递数据
        completeBlock(totalDtoArray.copy);
    
    } failureBlock:^(NSError *error) {
        completeBlock(nil);
    }];
    
}

@end







